import { login,loginScore, getRefereeInfo,logout, getInfo } from '@/api/login'
import { getTeamInfo } from '@/api/team'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    userInfo: {},
    teamInfo: {},
    refereeInfo:{},
    loginStatus: false
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_USERINFO: (state, userInfo) => {
      state.userInfo = userInfo
    },
    SET_TEAMINFO: (state, teamInfo) => {
      state.teamInfo = teamInfo
    },
    SET_REFEREEINFO: (state, refereeInfo) => {
      state.refereeInfo = refereeInfo
    },
    SET_LOGINSTATUS: (state, loginStatus) => {
      state.loginStatus = loginStatus
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password
      const type = userInfo.type
      return new Promise((resolve, reject) => {
        login(username, password,type).then(res => {
            setToken(res.data.token)
            commit('SET_TOKEN', res.data.token)
            commit('SET_LOGINSTATUS', true)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登录
    LoginScore({ commit }, refereeInfo) {
      const name = refereeInfo.name.trim()
      const idCard = refereeInfo.idCard
      return new Promise((resolve, reject) => {
        loginScore(name, idCard).then(res => {
          setToken(res.data.token)
          commit('SET_TOKEN', res.data.token)
          commit('SET_LOGINSTATUS', true)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          let data = res.data
          commit('SET_USERINFO', data)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取队伍信息
    GetTeamInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getTeamInfo().then(res => {
          let data = res.data
          commit('SET_TEAMINFO', data)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取裁判信息
    GetRefereeInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getRefereeInfo().then(res => {
          let data = res
          commit('SET_REFEREEINFO', data)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_LOGINSTATUS', false)
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        commit('SET_LOGINSTATUS', false)
        removeToken()
        resolve()
      })
    }
  }
}

export default user
